<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateQuestionsTopicTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('question_topic', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('question_id')->unsigned()->index();
          $table->integer('topic_id')->unsigned()->index();
            // 删除question时同时级联删除包含次question_id的记录
            $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
            // 同上
            $table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
          $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('question_topic');
    }
}
